package com.frikinet.lifeguard.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;

import org.hibernate.annotations.Cascade;

@Entity
public class Post
{
	@Id
	@GeneratedValue
	private long id;
	
	@Basic
	private String uid;
	
	@Basic
	private Date date;
	
	@Basic
	private String title;
	
	@Basic(fetch=FetchType.LAZY)
	@Lob
	private String text;
	
	@ManyToOne(cascade=CascadeType.ALL)
	private User user;
	
	@OneToMany(mappedBy = "post")
	@Cascade(org.hibernate.annotations.CascadeType.DELETE)
	@OrderBy("id")
	private List<Comment> comments = new ArrayList<Comment>();

	
	public String getUid()
	{
		return uid;
	}

	public void setUid(String uid)
	{
		this.uid = uid;
	}

	public Date getDate()
	{
		return date;
	}

	public void setDate(Date date)
	{
		this.date = date;
	}

	public long getId()
	{
		return id;
	}

	public void setId(long id)
	{
		this.id = id;
	}

	public String getTitle()
	{
		return title;
	}

	public void setTitle(String title)
	{
		this.title = title;
	}

	public String getText()
	{
		return text;
	}

	public void setText(String text)
	{
		this.text = text;
	}

	public List<Comment> getComments()
	{
		return comments;
	}

	public void setComments(List<Comment> comments)
	{
		this.comments = comments;
	}

	public User getUser()
	{
		return user;
	}

	public void setUser(User user)
	{
		this.user = user;
	}

	@Override
	public boolean equals(Object obj)
	{
		if(obj instanceof Post)
			return getUid().equals(((Post)obj).getUid());
		return false;
	}
}
